// Copyright (C) 1953-2022 NUDT
// Verilog module name - cpu_tx
// Version: V4.1.0.20221209
// Created:
//         by - fenglin
////////////////////////////////////////////////////////////////////////////
// Description:
//         
///////////////////////////////////////////////////////////////////////////

`timescale 1ns/1ps

module cpu_tx#(parameter local_module_id = 12'd0)
(
    i_clk  ,
    i_rst_n,
	
	iv_hcp_mid                            ,
	iv_os_cid                             ,
	
	iv_port_ptp_enabled                   ,    
    iv_measure_sync_state_report_enabled  ,
    i_cyclestart                          ,
    
    iv_frequency_cor                      ,

    iv_gm_timestamps         ,
    iv_correctfield_time     ,
    iv_local_cnt_rx          ,
    iv_sync_correctfield_time,
    iv_sync_link_delay       ,
    i_time_info_valid        , 

    iv_clk_set               ,
    i_clk_set_wr             ,
    iv_current_local_count   ,
    iv_current_sync_clk      ,
	
	i_sync_ok                             ,
	iv_sync_abnormal_cnt                  ,
	iv_offset                             ,
	i_sync_state_wr                       ,
    
    iv_csrateoffset_previousnode          ,
    i_csrateoffset_previousnode_wr        ,
	
	iv_fifo_data_count                    ,
    o_fifo_rden                           ,
    iv_fifo_rdata                         ,	

	ov_data                               ,
	o_data_wr                             ,
	ov_cyclestart_sequenceid
);

// I/O
// clk & rst
input                   i_clk;
input                   i_rst_n; 

input      [11:0]       iv_hcp_mid;
input      [11:0]       iv_os_cid ;
// pkt input                       
input      [31:0]       iv_port_ptp_enabled     ;
input                   iv_measure_sync_state_report_enabled;
input                   i_cyclestart;

input      [48:0]       iv_frequency_cor       ;

input     [79:0]	    iv_gm_timestamps         ;
input     [63:0]	    iv_correctfield_time     ;
input     [39:0]	    iv_local_cnt_rx          ;
input     [63:0]        iv_sync_correctfield_time;
input     [27:0]        iv_sync_link_delay       ;
input      	            i_time_info_valid        ; 

input     [95:0]	    iv_clk_set             ;
input    	            i_clk_set_wr           ;
input     [39:0]        iv_current_local_count ;
input     [95:0]        iv_current_sync_clk    ;

input                   i_sync_ok                         ;
input	   [15:0]	    iv_sync_abnormal_cnt              ;
input	   [12:0]       iv_offset                         ;
(*MARK_DEBUG="true"*)input                   i_sync_state_wr                   ;

input	   [31:0]       iv_csrateoffset_previousnode      ;
input                   i_csrateoffset_previousnode_wr    ;
// pkt output to NMA
input      [4:0]	    iv_fifo_data_count        ;
output reg	            o_fifo_rden               ;
input      [319:0]      iv_fifo_rdata             ;

output reg [8:0]        ov_data               ;
output reg              o_data_wr             ; 
output     [31:0]		ov_cyclestart_sequenceid;
//***************************************************
//               CycleStart SequenceID
//***************************************************
reg        [31:0]       rv_cyclestart_sequenceid;
always @(posedge i_clk or negedge i_rst_n) begin
    if(!i_rst_n)begin
		rv_cyclestart_sequenceid        <= 32'b0;                 
    end
    else begin
        if(i_sync_ok && i_cyclestart)begin	
			rv_cyclestart_sequenceid  <= rv_cyclestart_sequenceid + 1'b1; 
        end
        else begin
            rv_cyclestart_sequenceid  <= rv_cyclestart_sequenceid   ;  
        end		
    end
end	
assign	ov_cyclestart_sequenceid	  =	rv_cyclestart_sequenceid;
//***************************************************
//               register sync timestamps
//***************************************************
reg       [79:0]	    rv_gm_timestamps         ;
reg       [63:0]	    rv_correctfield_time     ;
reg       [39:0]	    rv_local_cnt_rx          ;
reg       [63:0]        rv_sync_correctfield_time;
reg       [27:0]        rv_sync_link_delay       ;
reg                     r_sync_timestamps_valid  ;
reg                     r_sync_timestamps_used   ;

reg       [95:0]	    rv_clk_set             ;
reg       [39:0]        rv_current_local_count ;
reg      	            r_tsc_timestamps_valid ;
reg      	            r_tsc_timestamps_used  ;
always @(posedge i_clk or negedge i_rst_n) begin
    if(!i_rst_n)begin
        rv_gm_timestamps                    <= 80'b0 ;
        rv_correctfield_time                <= 64'b0 ;	
		rv_local_cnt_rx                     <= 40'b0 ;
		rv_sync_correctfield_time           <= 64'b0 ;
		rv_sync_link_delay                  <= 28'b0 ;
        r_sync_timestamps_valid             <= 1'b0  ; 

        rv_clk_set                          <= 96'b0 ;
        rv_current_local_count              <= 40'b0 ;
        r_tsc_timestamps_valid              <= 1'b0  ;
    end
    else begin
        if(i_time_info_valid)begin	
            rv_gm_timestamps                    <= iv_gm_timestamps         ;
            rv_correctfield_time                <= iv_correctfield_time     ;	
            rv_local_cnt_rx                     <= iv_local_cnt_rx          ;
            rv_sync_correctfield_time           <= iv_sync_correctfield_time;
            rv_sync_link_delay                  <= iv_sync_link_delay       ;
            r_sync_timestamps_valid             <= 1'b1                     ;
        end
        else if(r_sync_timestamps_used)begin
            r_sync_timestamps_valid                      <= 1'b0              ; 
        end
        else begin
            r_sync_timestamps_valid                      <= r_sync_timestamps_valid   ;  
        end

        if(i_clk_set_wr)begin	
            rv_clk_set                          <= iv_clk_set ;                
            rv_current_local_count              <= iv_current_local_count ;                
            r_tsc_timestamps_valid              <= 1'b1  ;      
        end
        else if(r_tsc_timestamps_used)begin
            r_tsc_timestamps_valid              <= 1'b0              ; 
        end
        else begin
            r_tsc_timestamps_valid              <= r_tsc_timestamps_valid   ;  
        end			
    end
end
//***************************************************
//               register sync result
//***************************************************
reg                     r_sync_result_used                 ;

reg                     r_sync_ok                         ;
reg  	   [15:0]	    rv_sync_abnormal_cnt              ;
reg  	   [12:0]       rv_offset                         ;
reg  	   [31:0]       rv_cumulativescaledrateoffset     ;
reg        [95:0]       rv_current_sync_clk    ;
(*MARK_DEBUG="true"*)reg                      r_sync_result_valid                   ;
always @(posedge i_clk or negedge i_rst_n) begin
    if(!i_rst_n)begin
        r_sync_ok                            <= 1'b0 ;
        rv_sync_abnormal_cnt                 <= 16'b0;	
		rv_offset                            <= 13'b0;
		rv_cumulativescaledrateoffset        <= 32'b0;
		rv_current_sync_clk                  <= 96'b0 ;
		r_sync_result_valid                      <= 1'b0 ;                   
    end
    else begin
        if(i_sync_state_wr)begin	
			r_sync_ok                            <= i_sync_ok                    ;
			rv_sync_abnormal_cnt                 <= iv_sync_abnormal_cnt         ;	
			rv_offset                            <= iv_offset                    ;
			rv_cumulativescaledrateoffset        <= iv_csrateoffset_previousnode;
            rv_current_sync_clk                  <= iv_current_sync_clk ; 
			r_sync_result_valid                      <= 1'b1              ;  
        end
        else if(r_sync_result_used)begin
            r_sync_result_valid                      <= 1'b0              ; 
        end
        else begin
            r_sync_result_valid                      <= r_sync_result_valid   ;  
        end		
    end
end	
//***************************************************
//               packet generate
//***************************************************
reg       [48:0]              rv_report_frequency_cor       ;
reg       [31:0]              rv_report_cyclestart_sequenceid;
reg       [15:0]              rv_report_sync_state_sequence           ;
reg       [15:0]              rv_report_measure_state_sequence        ;

reg       [10:0]              rv_byte_cnt            ;
reg       [5:0]               rv_ts_cnt              ;
(*MARK_DEBUG="true"*)reg       [2:0]               rv_ctx_state           ;
localparam      IDLE_S                            = 3'd0,
                REPORT_SYNC_STATE_S               = 3'd1,
                REPORT_MEASURE_STATE_S            = 3'd2,
                TRANSMIT_MEASURE_TIMESTAMPS_S     = 3'd3;
always @(posedge i_clk or negedge i_rst_n) begin
    if(!i_rst_n)begin
        rv_report_sync_state_sequence           <= 16'b0;
        rv_report_measure_state_sequence        <= 16'b0;	
        rv_report_frequency_cor                 <= 49'b0;
        rv_report_cyclestart_sequenceid         <= 32'b0;
		r_sync_result_used               <= 1'b0 ;
        r_sync_timestamps_used           <= 1'b0 ;
        r_tsc_timestamps_used            <= 1'b0 ;
		rv_byte_cnt                      <= 8'b0 ;
		rv_ts_cnt                        <= 6'b0 ;
		o_fifo_rden                      <= 1'b0 ;
		
		ov_data                          <= 9'b0;
        o_data_wr                        <= 1'b0;
						                 
        rv_ctx_state                     <= IDLE_S;        
    end
    else begin
        case(rv_ctx_state)
            IDLE_S:begin
                r_sync_result_used        <= 1'b0 ;
                r_sync_timestamps_used    <= 1'b0 ;
                r_tsc_timestamps_used     <= 1'b0 ;				
				rv_byte_cnt               <= 8'd0 ;
				rv_ts_cnt                 <= 6'b0 ;
				o_fifo_rden               <= 1'b0 ;
				ov_data                   <= 9'b0 ;
				o_data_wr                 <= 1'b0 ;			
				if(iv_measure_sync_state_report_enabled)begin	
					if(r_sync_timestamps_valid && r_tsc_timestamps_valid && r_sync_result_valid)begin
						rv_report_sync_state_sequence    <= rv_report_sync_state_sequence + 1'b1;
						rv_ctx_state              <= REPORT_SYNC_STATE_S;  
					end
					else if((iv_fifo_data_count != 5'b0) && (iv_fifo_data_count >= (iv_port_ptp_enabled[0] + iv_port_ptp_enabled[1] + iv_port_ptp_enabled[2] + iv_port_ptp_enabled[3] + iv_port_ptp_enabled[4] + iv_port_ptp_enabled[5] + iv_port_ptp_enabled[6] + iv_port_ptp_enabled[7] + iv_port_ptp_enabled[8] + iv_port_ptp_enabled[9] + iv_port_ptp_enabled[10] + iv_port_ptp_enabled[11] + iv_port_ptp_enabled[12] + iv_port_ptp_enabled[13] + iv_port_ptp_enabled[14] + iv_port_ptp_enabled[15] + iv_port_ptp_enabled[16] + iv_port_ptp_enabled[17] + iv_port_ptp_enabled[18] + iv_port_ptp_enabled[19] + iv_port_ptp_enabled[20] + iv_port_ptp_enabled[21] + iv_port_ptp_enabled[22] + iv_port_ptp_enabled[23] + iv_port_ptp_enabled[24] + iv_port_ptp_enabled[25] + iv_port_ptp_enabled[26] + iv_port_ptp_enabled[27] + iv_port_ptp_enabled[28] + iv_port_ptp_enabled[29] + iv_port_ptp_enabled[30] + iv_port_ptp_enabled[31])))begin
						rv_report_measure_state_sequence <= rv_report_measure_state_sequence + 1'b1;	
						rv_ctx_state              <= REPORT_MEASURE_STATE_S;  
					end
					else begin
						rv_ctx_state    <= IDLE_S;  
					end
                end
                else begin
                    rv_ctx_state    <= IDLE_S;
                end				
            end
			REPORT_SYNC_STATE_S:begin
                rv_byte_cnt     <= rv_byte_cnt + 1'b1;
				rv_ts_cnt       <= 6'b0 ;
				o_data_wr       <= 1'b1;
				case(rv_byte_cnt)
				    //dmac
				    8'd0:ov_data          <= {1'b1,8'h66};
                    8'd1:ov_data          <= {1'b0,8'h26};
					8'd2:ov_data          <= {1'b0,8'h62};
					8'd3:ov_data          <= {1'b0,iv_os_cid[11:4]     };
					8'd4:ov_data          <= {1'b0,iv_os_cid[3:0 ],4'h8};
					8'd5:ov_data          <= {1'b0,8'h02};
					//smac                
					8'd6:ov_data          <= {1'b0, 8'h66                                 };
					8'd7:ov_data          <= {1'b0, 8'h26                                 };
					8'd8:ov_data          <= {1'b0, 8'h62                                 };
					8'd9:ov_data          <= {1'b0, iv_hcp_mid[11:4]                      };
					8'd10:ov_data         <= {1'b0, iv_hcp_mid[3:0 ],local_module_id[11:8]};
					8'd11:ov_data         <= {1'b0, local_module_id[7:0  ]                };
					//eth type
					8'd12:ov_data         <= {1'b0,8'hFF};
					8'd13:ov_data         <= {1'b0,8'h01};
					//TSMP类型
					8'd14:ov_data         <= {1'b0,8'h06};
					//TSMP子类�?
					8'd15:ov_data         <= {1'b0,8'h05};
					//OSM标识
					8'd16:ov_data         <= {1'b0,8'h00};
					//序列�?
					8'd17:ov_data         <= {1'b0,rv_report_sync_state_sequence[15:8]};
					8'd18:ov_data         <= {1'b0,rv_report_sync_state_sequence[7:0 ]};
					//时间�?
					8'd19:ov_data         <= {1'b0,rv_report_measure_state_sequence[15:8]};
					8'd20:ov_data         <= {1'b0,rv_report_measure_state_sequence[7:0]};
					8'd21:ov_data         <= {1'b0,8'h00};
					//保留
					8'd22:ov_data         <= {1'b0,8'h00};
					8'd23:ov_data         <= {1'b0,8'h00};
					8'd24:ov_data         <= {1'b0,8'h00};
					8'd25:ov_data         <= {1'b0,8'h00};
					8'd26:ov_data         <= {1'b0,8'h00};
					8'd27:ov_data         <= {1'b0,8'h00};
					8'd28:ov_data         <= {1'b0,8'h00};
					8'd29:ov_data         <= {1'b0,8'h00};
					8'd30:ov_data         <= {1'b0,8'h00};
					8'd31:ov_data         <= {1'b0,8'h00};
					//有效�?
					8'd32:ov_data         <= {1'b0,8'h01};
					//同步偏差异常计数�?
					8'd33:ov_data         <= {1'b0,rv_sync_abnormal_cnt[15:8]};
                    8'd34:ov_data         <= {1'b0,rv_sync_abnormal_cnt[7:0 ]};
					//sync_ok,同步偏差
					8'd35:ov_data         <= {1'b0,r_sync_ok,2'b0,rv_offset[12:8]};
					8'd36:ov_data         <= {1'b0,rv_offset[7:0 ]};
					//累计放大频差
					8'd37:ov_data         <= {1'b0,rv_cumulativescaledrateoffset[31:24]};
					8'd38:ov_data         <= {1'b0,rv_cumulativescaledrateoffset[23:16]};
					8'd39:ov_data         <= {1'b0,rv_cumulativescaledrateoffset[15:8 ]};
					8'd40:begin
					    r_sync_result_used <= 1'b1;
						ov_data           <= {1'b0,rv_cumulativescaledrateoffset[7:0  ]};
				    end
					//GM_timestamp
                    8'd41:begin
					    r_sync_result_used <= 1'b0;
						ov_data           <= {1'b0,rv_gm_timestamps[79:72]};
					end
					8'd42:ov_data         <= {1'b0,rv_gm_timestamps[71:64]};
					8'd43:ov_data         <= {1'b0,rv_gm_timestamps[63:56]};				
                    8'd44:ov_data         <= {1'b0,rv_gm_timestamps[55:48]};
					8'd45:ov_data         <= {1'b0,rv_gm_timestamps[47:40]};
					8'd46:ov_data         <= {1'b0,rv_gm_timestamps[39:32]};
					8'd47:ov_data         <= {1'b0,rv_gm_timestamps[31:24]};
					8'd48:ov_data         <= {1'b0,rv_gm_timestamps[23:16]};
					8'd49:ov_data         <= {1'b0,rv_gm_timestamps[15:8] };
					8'd50:ov_data         <= {1'b0,rv_gm_timestamps[7:0]  };
                    //correctfield_time
					8'd51:ov_data         <= {1'b0,rv_sync_correctfield_time[63:56]};
					8'd52:ov_data         <= {1'b0,rv_sync_correctfield_time[55:48]};
					8'd53:ov_data         <= {1'b0,rv_sync_correctfield_time[47:40]};
					8'd54:ov_data         <= {1'b0,rv_sync_correctfield_time[39:32]};
					8'd55:ov_data         <= {1'b0,rv_sync_correctfield_time[31:24]};
					8'd56:ov_data         <= {1'b0,rv_sync_correctfield_time[23:16]};
					8'd57:ov_data         <= {1'b0,rv_sync_correctfield_time[15:8] };
					8'd58:ov_data         <= {1'b0,rv_sync_correctfield_time[7:0]  };
                    //MAC_rx_timestamps
                    8'd59:ov_data         <= {1'b0,8'b0      };
					8'd60:ov_data         <= {1'b0,8'b0      };
					8'd61:ov_data         <= {1'b0,8'b0      };
					8'd62:ov_data         <= {1'b0,8'b0      };
					8'd63:ov_data         <= {1'b0,8'b0      };
					8'd64:ov_data         <= {1'b0,rv_local_cnt_rx[39:32]};
					8'd65:ov_data         <= {1'b0,rv_local_cnt_rx[31:24]};
					8'd66:ov_data         <= {1'b0,rv_local_cnt_rx[23:16]};
					8'd67:ov_data         <= {1'b0,rv_local_cnt_rx[15:8] };
					8'd68:ov_data         <= {1'b0,rv_local_cnt_rx[7:0]  };
                    //TSC_rx_timestamps
                    8'd69:ov_data         <= {1'b0,8'b0      };
					8'd70:ov_data         <= {1'b0,8'b0      };
					8'd71:ov_data         <= {1'b0,8'b0      };
					8'd72:ov_data         <= {1'b0,8'b0      };
					8'd73:ov_data         <= {1'b0,8'b0      };
					8'd74:ov_data         <= {1'b0,rv_current_local_count[39:32]};
					8'd75:ov_data         <= {1'b0,rv_current_local_count[31:24]};
					8'd76:ov_data         <= {1'b0,rv_current_local_count[23:16]};
					8'd77:ov_data         <= {1'b0,rv_current_local_count[15:8] };
					8'd78:ov_data         <= {1'b0,rv_current_local_count[7:0]  };
                    //link_delay
                    8'd79:ov_data         <= {1'b0,4'b0,rv_sync_link_delay[27:24] };
					8'd80:ov_data         <= {1'b0,rv_sync_link_delay[23:16]};
					//current_sync_clk
					8'd81:ov_data         <= {1'b0,rv_current_sync_clk[95:88]};
					8'd82:ov_data         <= {1'b0,rv_current_sync_clk[87:80]};
					8'd83:ov_data         <= {1'b0,rv_current_sync_clk[79:72]};
					8'd84:ov_data         <= {1'b0,rv_current_sync_clk[71:64]};
					8'd85:ov_data         <= {1'b0,rv_current_sync_clk[63:56]};
					8'd86:ov_data         <= {1'b0,rv_current_sync_clk[55:48]};
					8'd87:ov_data         <= {1'b0,rv_current_sync_clk[47:40]};
					8'd88:ov_data         <= {1'b0,rv_current_sync_clk[39:32]};
                    8'd89:ov_data         <= {1'b0,rv_current_sync_clk[31:24]};
					8'd90:ov_data         <= {1'b0,rv_current_sync_clk[23:16]};
					//clk_set
					8'd91 :ov_data         <= {1'b0,rv_clk_set[95:88] };
					8'd92 :ov_data         <= {1'b0,rv_clk_set[87:80] };
					8'd93 :ov_data         <= {1'b0,rv_clk_set[79:72]};
					8'd94 :ov_data         <= {1'b0,rv_clk_set[71:64]};
					8'd95 :ov_data         <= {1'b0,rv_clk_set[63:56]};
					8'd96 :ov_data         <= {1'b0,rv_clk_set[55:48]};
					8'd97 :ov_data         <= {1'b0,rv_clk_set[47:40]};
					8'd98 :ov_data         <= {1'b0,rv_clk_set[39:32]};
                    8'd99 :ov_data         <= {1'b0,rv_clk_set[31:24]};
                    8'd100:begin
                        ov_data         <= {1'b0,rv_clk_set[23:16]};
                        r_sync_timestamps_used    <= 1'b1 ;
                        r_tsc_timestamps_used     <= 1'b1 ;	
                        
                        rv_report_cyclestart_sequenceid <= rv_cyclestart_sequenceid;
                    end
                    //CycleStart_SequenceID
                    8'd101:begin
                        ov_data         <= {1'b0,rv_report_cyclestart_sequenceid[31:24]};
                        r_sync_timestamps_used    <= 1'b0 ;
                        r_tsc_timestamps_used     <= 1'b0 ;	                        
                    end
                    8'd102:ov_data         <= {1'b0,rv_report_cyclestart_sequenceid[23:16]};
                    8'd103:ov_data         <= {1'b0,rv_report_cyclestart_sequenceid[15:8] };
                    8'd104:ov_data         <= {1'b0,rv_report_cyclestart_sequenceid[7:0]  };
                    //Tick_length
                    8'd105:begin
                        ov_data          <= {1'b0,8'b0};
                        rv_report_frequency_cor <= iv_frequency_cor;
                    end
                    8'd106:ov_data         <= {1'b0,7'b0,rv_report_frequency_cor[48]};
                    8'd107:ov_data         <= {1'b0,rv_report_frequency_cor[47:40]};
                    8'd108:ov_data         <= {1'b0,rv_report_frequency_cor[39:32]};
                    8'd109:ov_data         <= {1'b0,rv_report_frequency_cor[31:24]};
                    8'd110:ov_data         <= {1'b0,rv_report_frequency_cor[23:16]};
                    8'd111:ov_data         <= {1'b0,rv_report_frequency_cor[15:8] };
                    8'd112:begin//last 1Byte
                        ov_data         <= {1'b1,rv_report_frequency_cor[7:0]  };
                        rv_ctx_state    <= IDLE_S;
                    end  
                endcase				
			end
            REPORT_MEASURE_STATE_S:begin	           
                rv_byte_cnt     <= rv_byte_cnt + 1'b1;
				rv_ts_cnt       <= 6'b0 ;
				o_data_wr       <= 1'b1;
				case(rv_byte_cnt)
				    //dmac
				    8'd0:ov_data          <= {1'b1,8'h66};
                    8'd1:ov_data          <= {1'b0,8'h26};
					8'd2:ov_data          <= {1'b0,8'h62};
					8'd3:ov_data          <= {1'b0,iv_os_cid[11:4]     };
					8'd4:ov_data          <= {1'b0,iv_os_cid[3:0 ],4'h8};
					8'd5:ov_data          <= {1'b0,8'h02};
					//smac                
					8'd6:ov_data          <= {1'b0, 8'h66                                 };
					8'd7:ov_data          <= {1'b0, 8'h26                                 };
					8'd8:ov_data          <= {1'b0, 8'h62                                 };
					8'd9:ov_data          <= {1'b0, iv_hcp_mid[11:4]                      };
					8'd10:ov_data         <= {1'b0, iv_hcp_mid[3:0 ],local_module_id[11:8]};
					8'd11:ov_data         <= {1'b0, local_module_id[7:0  ]                };
					//eth type
					8'd12:ov_data         <= {1'b0,8'hFF};
					8'd13:ov_data         <= {1'b0,8'h01};
					//TSMP类型
					8'd14:ov_data         <= {1'b0,8'h06};
					//TSMP子类�?
					8'd15:ov_data         <= {1'b0,8'h06};
					//OSM标识
					8'd16:ov_data         <= {1'b0,8'h00};
					//序列�?
					8'd17:ov_data         <= {1'b0,rv_report_sync_state_sequence[15:8]};
					8'd18:ov_data         <= {1'b0,rv_report_sync_state_sequence[7:0 ]};
					//时间�?
					8'd19:ov_data         <= {1'b0,rv_report_measure_state_sequence[15:8]};
					8'd20:ov_data         <= {1'b0,rv_report_measure_state_sequence[7:0]};
					8'd21:ov_data         <= {1'b0,8'h00};
					//保留
					8'd22:ov_data         <= {1'b0,8'h00};
					8'd23:ov_data         <= {1'b0,8'h00};
					8'd24:ov_data         <= {1'b0,8'h00};
					8'd25:ov_data         <= {1'b0,8'h00};
					8'd26:ov_data         <= {1'b0,8'h00};
					8'd27:ov_data         <= {1'b0,8'h00};
					8'd28:ov_data         <= {1'b0,8'h00};
					8'd29:ov_data         <= {1'b0,8'h00};
					8'd30:ov_data         <= {1'b0,8'h00};
					8'd31:ov_data         <= {1'b0,8'h00};
					//端口PTP使能
					8'd32:ov_data         <= {1'b0,iv_port_ptp_enabled[31:24]};
					8'd33:ov_data         <= {1'b0,iv_port_ptp_enabled[23:16]};
                    8'd34:ov_data         <= {1'b0,iv_port_ptp_enabled[15:8 ]};
					8'd35:begin
					    ov_data         <= {1'b0,iv_port_ptp_enabled[7:0  ]};
						rv_ctx_state    <= TRANSMIT_MEASURE_TIMESTAMPS_S;
				    end
				endcase
			end
			TRANSMIT_MEASURE_TIMESTAMPS_S:begin
				rv_byte_cnt   <= rv_byte_cnt + 1'b1;
				if(rv_ts_cnt >= 6'd39)begin
				    rv_ts_cnt     <= 6'd0 ;
				end
				else begin
				    rv_ts_cnt     <= rv_ts_cnt + 1'b1 ;
				end
				case(rv_ts_cnt)
					//t1
					6'd0: ov_data         <= {1'b0,iv_fifo_rdata[79:72]};
					6'd1: ov_data         <= {1'b0,iv_fifo_rdata[71:64]};
					6'd2: ov_data         <= {1'b0,iv_fifo_rdata[63:56]};
					6'd3: ov_data         <= {1'b0,iv_fifo_rdata[55:48]};
					6'd4: ov_data         <= {1'b0,iv_fifo_rdata[47:40]};
					6'd5: ov_data         <= {1'b0,iv_fifo_rdata[39:32]};
					6'd6: ov_data         <= {1'b0,iv_fifo_rdata[31:24]};
					6'd7: ov_data         <= {1'b0,iv_fifo_rdata[23:16]};
                    6'd8: begin
					    //o_fifo_rden     <= 1'b1;
                        o_fifo_rden     <= 1'b0;
						ov_data         <= {1'b0,iv_fifo_rdata[15:8 ]};
					end
					6'd9: begin
					    o_fifo_rden     <= 1'b0;
					    ov_data         <= {1'b0,iv_fifo_rdata[7:0  ]};
					end
					//t2
					6'd10:ov_data         <= {1'b0,iv_fifo_rdata[159:152]};
					6'd11:ov_data         <= {1'b0,iv_fifo_rdata[151:144]};
					6'd12:ov_data         <= {1'b0,iv_fifo_rdata[143:136]};
					6'd13:ov_data         <= {1'b0,iv_fifo_rdata[135:128]};
					6'd14:ov_data         <= {1'b0,iv_fifo_rdata[127:120]};
					6'd15:ov_data         <= {1'b0,iv_fifo_rdata[119:112]};
					6'd16:ov_data         <= {1'b0,iv_fifo_rdata[111:104]};
					6'd17:ov_data         <= {1'b0,iv_fifo_rdata[103:96 ]};
                    6'd18:begin
					    //o_fifo_rden     <= 1'b1;
                        o_fifo_rden     <= 1'b0;
						ov_data         <= {1'b0,iv_fifo_rdata[95:88 ]};
					end
					6'd19:begin
					    o_fifo_rden     <= 1'b0;
					    ov_data         <= {1'b0,iv_fifo_rdata[87:80 ]};
					end	
					//1st-t3
					6'd20:ov_data         <= {1'b0,iv_fifo_rdata[239:232]};
					6'd21:ov_data         <= {1'b0,iv_fifo_rdata[231:224]};
					6'd22:ov_data         <= {1'b0,iv_fifo_rdata[223:216]};
					6'd23:ov_data         <= {1'b0,iv_fifo_rdata[215:208]};
					6'd24:ov_data         <= {1'b0,iv_fifo_rdata[207:200]};
					6'd25:ov_data         <= {1'b0,iv_fifo_rdata[199:192]};
					6'd26:ov_data         <= {1'b0,iv_fifo_rdata[191:184]};
					6'd27:ov_data         <= {1'b0,iv_fifo_rdata[183:176 ]};
                    6'd28:begin
					    //o_fifo_rden     <= 1'b1;
                        o_fifo_rden     <= 1'b0;
						ov_data         <= {1'b0,iv_fifo_rdata[175:168 ]};
					end
					6'd29:begin
					    o_fifo_rden     <= 1'b0;
					    ov_data         <= {1'b0,iv_fifo_rdata[167:160 ]};
					end	
					//1st-t4
					6'd30:ov_data         <= {1'b0,iv_fifo_rdata[319:312]};
					6'd31:ov_data         <= {1'b0,iv_fifo_rdata[311:304]};
					6'd32:ov_data         <= {1'b0,iv_fifo_rdata[303:296]};
					6'd33:ov_data         <= {1'b0,iv_fifo_rdata[295:288]};
					6'd34:ov_data         <= {1'b0,iv_fifo_rdata[287:280]};
					6'd35:ov_data         <= {1'b0,iv_fifo_rdata[279:272]};
					6'd36:ov_data         <= {1'b0,iv_fifo_rdata[271:264]};
					6'd37:ov_data         <= {1'b0,iv_fifo_rdata[263:256 ]};
                    6'd38:begin
					    o_fifo_rden     <= 1'b1;
						ov_data         <= {1'b0,iv_fifo_rdata[255:248 ]};
					end
					6'd39:begin
					    o_fifo_rden     <= 1'b0;
					    ov_data[7:0]    <= iv_fifo_rdata[247:240 ];
						if(iv_fifo_data_count == 5'd1)begin
						    ov_data[8]    <= 1'b1;
                            rv_ctx_state  <= IDLE_S;
						end
						else begin
						    ov_data[8]    <= 1'b0;
                            rv_ctx_state  <= TRANSMIT_MEASURE_TIMESTAMPS_S;
						end
					end
                    default:begin
                        o_fifo_rden     <= 1'b0;
						rv_ctx_state    <= TRANSMIT_MEASURE_TIMESTAMPS_S;
                    end					
                endcase
            end	          
            default:begin
                r_sync_result_used         <= 1'b0 ;				
				rv_byte_cnt               <= 8'd0 ;
				rv_ts_cnt                 <= 6'b0 ;
				o_fifo_rden               <= 1'b0 ;
				ov_data                   <= 9'b0 ;
				o_data_wr                 <= 1'b0 ;				
				rv_ctx_state              <= IDLE_S;  
            end
        endcase
    end
end
endmodule